
<%@page import="javax.xml.transform.stream.StreamResult"%>
<%@page import="javax.xml.transform.*"%>
<%@page import="java.text.*"%>
<%@page import="java.net.URI"%>
<%@page import="com.ctc.wstx.io.CharArraySource"%>
<%@page import="javax.xml.transform.stream.StreamSource"%>
<%@page import="org.w3c.dom.*"%>
<%@page import="org.apache.xerces.dom.*"%>
<%@page import="java.io.*"%>
<%@page import="java.util.HashMap"%>
<%@page import="sessionBean.*"%>
<%@page import="java.util.*"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="DBConnection.ATeamObject"%>
<%@page import="manufacturerBean.*"%>
<%@page import="goodsBean.*"%>
<%@page import="categoryBean.*"%>
<%@ page errorPage = "Error.jsp" %>
<%-- 
    Document   : export
    Created on : Nov 27, 2011, 7:18:42 PM
    Author     : XPYCT
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
        <%  Collection<Integer> exportKeys =
                (Collection<Integer>)request.getSession().
                getAttribute("exportKeys");            
            
            Object homeSessionObject;
            SessionHome sessionHome;
            Session sessionObject;   
            
            homeSessionObject = new InitialContext().lookup("SessionBean");
            sessionHome = (SessionHome)PortableRemoteObject.narrow(
                homeSessionObject,SessionHome.class);
            sessionObject = sessionHome.create();
            
            HashMap<Integer, Collection> keys =
                    new HashMap<Integer, Collection>();
            keys.put(0, exportKeys);
                        
            CharArrayWriter caw = new CharArrayWriter();
            StreamResult sr = new StreamResult(caw);
    
            TransformerFactory transformerFactory =
                    TransformerFactory.newInstance();
            File xslfile = new File(application.getRealPath("/")+
                    "ExportStylesheet.xsl");
            
            Transformer transformer = transformerFactory.newTransformer(
                    new StreamSource(xslfile));
            transformer.transform(sessionObject.getXML(keys), sr);

            CharArrayReader car = new CharArrayReader(caw.toCharArray());
            BufferedReader fb = new BufferedReader(car);
            String instring;
            
            while((instring = fb.readLine()) != null){
        %>
        <%= instring %>
        <%  }
            caw.reset();
                        
            transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(
                    OutputKeys.OMIT_XML_DECLARATION, "no");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(sessionObject.getXML(keys), sr);
            
                      
            String xmlPath = application.getRealPath("/") + 
                     "export" + ".xml";
            request.getSession().setAttribute("xmlPath", xmlPath);
            File xmlfile = new File(xmlPath);
            
            Writer writer = new FileWriter (xmlfile);
            PrintWriter rw = new PrintWriter(writer);
            rw.print(caw.toString());
            rw.flush(); 
            rw.close();
            caw.close();
            car.close();
            fb.close();
        %>